﻿using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NDBGen.Configuration;
using NDBGen.Plugins;

namespace NDBGen.Tests
{
    [TestClass]
    public abstract class UnitTestBase<TPluginData>
        where TPluginData : IPluginData
    {
        public class CompositionTester<TContract, TMetadata>
        {
            [ImportMany]
            public Lazy<TContract, TMetadata>[] Imports { get; set; }
        }

        protected abstract TPluginData GetPluginData(ParsedInput parsedInput);

        public TestContext TestContext { get; set; }

        [TestInitialize]
        public virtual void TestInitialize()
        {
        }

        protected void CompareLines(String expectedContent, String actualContent)
        {
            var split = new Func<String, String[]>(s => s.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
            var expectedLines = split.Invoke(expectedContent);
            var actualLines = split.Invoke(actualContent);

            for (var lineNumber = 1; lineNumber <= expectedLines.Length; lineNumber++)
            {
                if (lineNumber > actualLines.Length) Assert.AreEqual(expectedContent, actualContent, "Expected content had more lines at line {0}", lineNumber);

                var actual = actualLines[lineNumber - 1];
                var expected = expectedLines[lineNumber - 1];

                if (!actual.Equals(expected))
                {
                    Assert.AreEqual(expectedContent, actualContent, false, "Content mismatch at line {0}", lineNumber);
                }
            }

            if (actualLines.Length > expectedLines.Length) Assert.AreEqual(expectedContent, actualContent, "Actual content had unexpected content after line {0}", actualLines.Length);
        }
    }
}